<?php

!defined('IN_ROOT') && exit('Access Denied');

class admin_linecontrol extends base {

    function admin_linecontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load('line');
        $this->load('cate');
        $this->load('area');
        $this->load('sysmap');
    }

    function ondefault($message='') {
        if(empty($message)) unset($message);
        @$page = max(1, intval($this->get[2]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $linelist = $_ENV['line']->get_list($startindex,$pagesize);
        $viewsnum=$this->db->fetch_total('line');
        $departstr=page($viewsnum, $pagesize, $page,"admin_line/default");
        include template('linelist','admin');
    }

    function onadd() {
    	$uid = (isset($this->get[2]))?intval($this->get[2]):$this->post['uid'];
        if(isset($this->post['uid'])) {
            $title = $this->post['title'];
            $topid = $this->post['topid'];
            foreach ($topid as $tk => $tv) {
            	$cate_{$tk} = $this->post["cate_$tk"];
            	if(count($cate_{$tk}) ==0){
            		msg($tv."不能为空");
            	}
            }
            !$title && msg("线路名称不能为空");
            $pid = (int)$this->post['pid'];
            $pid == 0 && msg("请选择出发城市");
            $views = $this->post['views'];
            count($views) ==0 && msg("请选择途经景点");
            $earlier_date = (int)$this->post['earlier_date'];
            $earlier_date == 0 && msg("提前报名的天数不能为空");
            $daynum = (int)$this->post['daynum'];
            $daynum == 0 && msg("行程天数不能为空");
            $group_date = (int)$this->post['group_date'];
            $traffic = $this->post['traffic'];
            !$traffic && msg("交通方式不能为空");
            $orderid = (int)$this->post['orderid'];
            $isrec = (int)$this->post['isrec'];
            $pics = '';
            if($_FILES['imgurl']['name']){
            	$imgname=strtolower($_FILES['imgurl']['name']);
            	$type = substr(strrchr($imgname,'.'),1);
            	if(!isimage($type)) {
	                msg("图片格式不支持，目前仅支持jpg、gif、png格式！");
	            }
	            $pics = 'v'.substr(md5(random(3,0).time()),0,9).'.'.$type;
	            $filepath = '/data/attach/lineimg/'.$pics;
	            forcemkdir(MY_ROOT.'/data/attach/lineimg');
	            move_uploaded_file($_FILES['imgurl']['tmp_name'],MY_ROOT.$filepath);
            }
            $lid = $_ENV['line']->add($uid,$title,$earlier_date,$pid,$group_date,$daynum,$traffic,$pics,$orderid,$isrec);
            //$this->cache->remove('line');
            $_ENV['sysmap']->dellinecate($lid);
            foreach ($topid as $tk => $tv) {
            	$_ENV['sysmap']->inlinecate($lid,$cate_{$tk});
            }
            $_ENV['sysmap']->dellineview($lid);
            $_ENV['sysmap']->inlineview($lid,$views);
            $_ENV['sysmap']->uplineindex($uid,$lid);
            //$this->ondefault('添加成功！');
            msg("添加成功，跳转到第二步","index.php?admin_line/editprice/$lid");
        }else {
        	$catestr = $_ENV['cate']->get_cateshow_admin();//分类
        	$viewstr = $_ENV['sysmap']->get_user_area_show($uid);
        	$area_js=$_ENV['area']->select_option_cate();
            include template('addline','admin');
        }
    }

    function onedit() {
    	
        if(isset($this->post['lid'])) {
        	$uid = (int)$this->post['uid'];
        	$lid = (int)$this->post['lid'];
            $title = $this->post['title'];
            $topid = $this->post['topid'];
            foreach ($topid as $tk => $tv) {
            	$cate_{$tk} = $this->post["cate_$tk"];
            	if(count($cate_{$tk}) ==0){
            		msg($tv."不能为空");
            	}
            }
            !$title && msg("线路名称不能为空");
            $pid = (int)$this->post['pid'];
            $pid == 0 && msg("请选择出发城市");
            $views = $this->post['views'];
            count($views) ==0 && msg("请选择途经景点");
            $earlier_date = (int)$this->post['earlier_date'];
            $earlier_date == 0 && msg("提前报名的天数不能为空");
            $daynum = (int)$this->post['daynum'];
            $daynum == 0 && msg("行程天数不能为空");
            $group_date = (int)$this->post['group_date'];
            $traffic = $this->post['traffic'];
            !$traffic && msg("交通方式不能为空");
            $orderid = (int)$this->post['orderid'];
            $isrec = (int)$this->post['isrec'];
            $pics = '';
            if($_FILES['imgurl']['name']){
            	$imgname=strtolower($_FILES['imgurl']['name']);
            	$type = substr(strrchr($imgname,'.'),1);
            	if(!isimage($type)) {
	                msg("图片格式不支持，目前仅支持jpg、gif、png格式！");
	            }
	            $pics = 'v'.substr(md5(random(3,0).time()),0,9).'.'.$type;
	            $filepath = '/data/attach/lineimg/'.$pics;
	            forcemkdir(MY_ROOT.'/data/attach/lineimg');
	            move_uploaded_file($_FILES['imgurl']['tmp_name'],MY_ROOT.$filepath);
            }
            $_ENV['line']->update($lid,$uid,$title,$earlier_date,$pid,$group_date,$daynum,$traffic,$pics,$orderid,$isrec);
            //$this->cache->remove('line');
            $_ENV['sysmap']->dellinecate($lid);
            foreach ($topid as $tk => $tv) {
            	$_ENV['sysmap']->inlinecate($lid,$cate_{$tk});
            }
            $_ENV['sysmap']->dellineview($lid);
            $_ENV['sysmap']->inlineview($lid,$views);
            $_ENV['sysmap']->uplineindex($uid,$lid);
            $tz = (int)$this->post['tz'];
			if($tz == 1) {
				msg("编辑成功","index.php?admin_line/edit/$lid");
			}
			elseif($tz == 2) {
				msg("编辑成功","index.php?admin_line");
			}
			else{
				msg("编辑成功","index.php?admin_line/editprice/$lid");
			}
            //msg("编辑成功","index.php?admin_line/edit/$lid");
        }else {
        	$lid = intval($this->get[2]);
            $d = $_ENV['line']->get($lid);
            $catestr = $_ENV['cate']->get_cateshow_admin($lid);//分类
            $viewstr = $_ENV['sysmap']->get_user_area_show($d[uid],$lid);
        	$area_js=$_ENV['area']->select_option_cate($d['upareaid']);
            include template('editline','admin');
        }
    }

    function onremove() {
        $_ENV['line']->remove_by_id(intval($this->get[2]));
        msg("刪除成功！","index.php?admin_line");
    }
	function oneditprice() {
		$lid = intval($this->get[2]);
		$job = $this->get[3];
		$pid = intval($this->get[4]);
		$d = $_ENV['line']->get($lid);
		$list = $_ENV['line']->getpricelist($lid);
		if($job == 'edit'){
			$dp = $_ENV['line']->get_prcie($pid);
		}
		include template('lineprice','admin');
	}
	function oneditpricesave() {
		if(isset($this->post['lid'])) {
			$typename = $this->post['typename'];
			!$typename && msg("价格类型不能为空") ;
			$price = (int)$this->post['price'];
			$price_my = (int)$this->post['price_my'];
			!$price_my && msg("优惠价不能为空") ;
			$price_child = (int)$this->post['price_child'];
			$descs = $this->post['descs'];
			!$descs && msg("费用说明不能为空") ;
			$views = (int)$this->post['views'];
			$lid = (int)$this->post['lid'];
			$_ENV['line']->addprice($lid,$typename,$price,$price_my,$price_child,$descs,$views);
			$tz = (int)$this->post['tz'];
			if($tz == 1) {
				msg("添加成功","index.php?admin_line/editprice/$lid");
			}else{
				msg("添加成功","index.php?admin_line/editday/$lid");
			}
	        
		}else{
			msg("error!");
		}
		
	}
	function oneditpriceedit() {
		if(isset($this->post['pid'])) {
			$typename = $this->post['typename'];
			!$typename && msg("价格类型不能为空") ;
			$price = (int)$this->post['price'];
			$price_my = (int)$this->post['price_my'];
			!$price_my && msg("优惠价不能为空") ;
			$price_child = (int)$this->post['price_child'];
			$descs = $this->post['descs'];
			!$descs && msg("费用说明不能为空") ;
			$views = (int)$this->post['views'];
			$lid = (int)$this->post['lid'];
			$pid = (int)$this->post['pid'];
			$_ENV['line']->upprice($pid,$lid,$typename,$price,$price_my,$price_child,$descs,$views);
			$tz = (int)$this->post['tz'];
			if($tz == 1) {
				msg("编辑成功","index.php?admin_line/editprice/$lid");
			}else{
				msg("编辑成功","index.php?admin_line/editday/$lid");
			}
		}else{
			msg("error!");
		}
		
	}
	function oneditday()
	{
		if(isset($this->post['lid'])) {
			$lid = intval($this->post['lid']);
			$descs = $this->post['features'];
			$type =  intval($this->post['sch_tc']);
			$content=$this->post['content'];
			if($type == 1) {
				$att=$this->post['att'];
				$_ENV['line']->updays($lid,$att);
			}
			$_ENV['line']->upday($lid,$descs,$type,$content);
			$tz = (int)$this->post['tz'];
			if($tz == 1) {
				msg("编辑成功","index.php?admin_line/editday/$lid");
			}else{
				msg("编辑成功","index.php?admin_line/editdesc/$lid");
			}
			
		}else{
			$lid = intval($this->get[2]);
			$d = $_ENV['line']->get($lid);
			$ds = $_ENV['line']->get_day($lid);
			$list = $_ENV['line']->get_days($lid);
			$dstr = $_ENV['line']->get_dayshow($lid,$d[daynum]);
			include template('lineday','admin');
		}
		
	}
	function oneditdesc() {
		if(isset($this->post['lid'])) {
			$lid = intval($this->post['lid']);
			$xztitle = $this->post['xztitle'];
			$xzcontent = $this->post['xzcontent'];
			$tstitle = $this->post['tstitle'];
			$tscontent = $this->post['tscontent'];
			$_ENV['line']->updescs($lid,$xztitle,$xzcontent,$tstitle,$tscontent);
			$tz = (int)$this->post['tz'];
			if($tz == 1) {
				msg("编辑成功","index.php?admin_line/editdesc/$lid");
			}else{
				msg("编辑成功","index.php?admin_line");
			}
			//msg("编辑成功","index.php?admin_line/editdesc/$lid");
		}else{
			$lid = intval($this->get[2]);
			$d = $_ENV['line']->get_descs($lid);
			include template('linedesc','admin');
		}
	}
    function onreorder() {
        $orders = explode(",",$this->post['order']);
        $hid = intval($this->post['hiddencid']);
        foreach($orders as $order => $lid) {
            $_ENV['line']->order_line(intval($lid),$order);
        }
    }
    function ondelprice() {
    	$_ENV['line']->del_price(intval($this->get[3]));
        msg("删除成功","index.php?admin_line/editprice/".intval($this->get[2]));
    }

}
?>